package com.grom.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.dispacher.context.ContextHolder;
import com.grom.po.Target;
import com.grom.service.TargetService;
import com.grom.util.JsonUtil;
import com.holder.log.ConsoleLog;

public class TargetAction {
	public void gotoList() {
		List<Target> allTargets = TargetService.instance.getAllTargets();
		ContextHolder.setRequestAttribute("allTargets", allTargets);
		ContextHolder.forward("/WEB-INF/jsp/Target/update.jsp");
	}

	public void doSave() {
		Target object = (Target) ContextHolder.initFromTheRequest(Target.class);
		if (object.getID() == null || object.getID().trim().equals("")) {
			TargetService.instance.add(object);
		} else {
			TargetService.instance.update(object);
		}
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri() + "/TargetAction/gotoList.do");
	}

	public void getDetail() {
		String id = ContextHolder.findInstance().getRequest().getParameter("itemid");
		Target object = TargetService.instance.getTargetById(id);
		String json = JsonUtil.parseToJson(object);
		ConsoleLog.debug("| TargetAction | getDetail | json | " + json);
		HttpServletResponse response = ContextHolder.findInstance().getResponse();
		try {
			PrintWriter out = response.getWriter();
			out.write("Target=" + json);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void doDelete() {
		String id = ContextHolder.findInstance().getRequest().getParameter("deleteId");
		System.out.println("| TargetAction | doDelete | id | " + id);
		Target object = TargetService.instance.getTargetById(id);
		TargetService.instance.delete(object);
		ContextHolder.sendRedirect(ContextHolder.findInstance().getContextUri() + "/TargetAction/gotoList.do");
	}

	public void conditionSelect() {
		String clothType_s = ContextHolder.findInstance().getRequest().getParameter("clothType_s");
		String brandName_s = ContextHolder.findInstance().getRequest().getParameter("brandName_s");
		String sql = "select * from Target where clothType like '%" + clothType_s + "%' and brandName like '%"
				+ brandName_s + "%'";
		List<Target> allTargets = TargetService.instance.selectByCondition(sql);
		ContextHolder.setRequestAttribute("allTargets", allTargets);
		ContextHolder.forward("/WEB-INF/jsp/Target/update.jsp");
	}
}
